window.onload = function () {

    //登录验证
    $('.login-btn').click(function () {

        // let forms = document.forms[0].elements;
        // let accVal = forms[0].value.trim();
        // let pwdVal = forms[1].value.trim();

        let zh = $('.account-input').val();
        let mm = $('.password-input').val();
        // console.log(zh,mm);
        //正则表达式
        let reg = /^[a-zA-Z0-9]{5,10}$/
        let age = /^[a-zA-Z0-9]{5,12}$/
        let isAccount = reg.test(zh);
        let isPassword = age.test(mm);
        
        if (isAccount) {
            $('.form-box .acc').css('display', 'none');
        } else {
            $('.account-input').css('border', '1px solid #ff6700');
            $('.acc').css('display', 'block');
            $(".account-input").val("");
        }

        if (isPassword) {
        } else {
            $('.password-input').css('border', '1px solid #ff6700');
            $('.form-box .acc').css('display', 'block');
            $(".password-input").val("");
        }

        axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
        let pinJie = `username=${isAccount}&password=${isPassword}`;
        axios.post('http://localhost:8888/users/login', pinJie).then(res => {
            // console.log(res);
            // #判断登录状态,将用户信息进行保存
            if (res.status == 200 && res.data.code == 1) {
                //将token和user保存到local中
                localStorage.setItem('token', res.data.token);
                localStorage.setItem('user_id', res.data.user.id);
                localStorage.setItem('nickname', res.data.user.nickname);
                //! 如果有会跳的地址,则跳转
                if (this.url) {
                    location.assign('./index.html')
                }

            }

        })




    });



}
